package com.wensen.service;

import com.wensen.entity.BankInfo;
import com.wensen.feigin.ProductFeign;
import com.wensen.mapper.BankInfoMapper;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class BankInfoService {

    @Autowired
    private BankInfoMapper bankInfoMapper;

    @Autowired
    ProductFeign feign;

    @Autowired
    RedisTemplate redisTemplate;

    @GlobalTransactional(rollbackFor = Exception.class)
    public void updateBankInfoById(int id){
        // 获取 XID
        String xid = RootContext.getXID();
        System.out.println("user===XID==="+xid);
        bankInfoMapper.updateBankInfoById(id);
        String result = feign.getProductInfo();
        System.out.println(1/0);
        System.out.println("=======结束");
    }

    public Object getById(int id){
        String key = "bankInfo:"+id;
        Object bankInfo = redisTemplate.opsForValue().get(key);
        if (bankInfo == null) {
            synchronized (this.getClass()){
                bankInfo = redisTemplate.opsForValue().get(key);
                if (bankInfo == null) {
                    System.out.println("redis 没有，查询数据库去====");
                    BankInfo bankInfo1 = bankInfoMapper.getBankInfoById(id);
                    redisTemplate.opsForValue().set(key,bankInfo1);
                    return bankInfo1;
                } else {
                    return bankInfo;
                }
            }
        }
        return bankInfo;
    }

}
